///******************** NG.Respate(C)  **********************************
//* 产品名称: Respate 科技
//* 文件名称: signal.c
//* 程序作者: 雷帕特公司(南宫子萍)
//* 程序版本: V1.1														   
//* 编制日期: 2019/1/29
//* 功能描述: 系统日志
//* QQ:	2085827949
//**********************************************************************/

// #include "flight.h"
#define _XOPEN_SOURCE       /* See feature_test_macros(7) */

#include "stdio.h"
#include "stdlib.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "lap_lib.h"

#include <pwd.h>
#include "laperro.h"
      

/* The passwd structure.
struct passwd
{
  char *pw_name;		/* Username.
  char *pw_passwd;		/* Password.
  __uid_t pw_uid;		/* User ID.
  __gid_t pw_gid;		/* Group ID.
  char *pw_gecos;		/* Real name
  char *pw_dir;			/* Home directory.
  char *pw_shell;		/* Shell program.
};*/
#include <pwd.h>
// 获取用户信息(/etc/passwd 文件信息)
void LAP_getPWNAM(void)
{
  struct passwd * pwd = getpwnam("respate");  // getpwnam 与 getpwuid函数不可重入(可重入:getpwnam_r 与 getpwuid_r)
  printf("pw_name:%s\r\n",pwd->pw_name);      // 登录名称
  printf("pw_passwd:%s\r\n",pwd->pw_passwd);  // 登录密码(空字符:无密码 非空:启用了 shadow,者密码存放于/etc/shadow)
  printf("pw_uid:%d\r\n",pwd->pw_uid);        // 用户ID
  printf("pw_gid:%d\r\n",pwd->pw_gid);        // 组ID
  printf("pw_gecos:%s\r\n",pwd->pw_gecos);    // 用户真实名称
  printf("pw_dir:%s\r\n",pwd->pw_dir);        // 家目录
  printf("pw_shell:%s\r\n",pwd->pw_shell);    // 登录shell(若为空，则使用默认shell(/bin/sh))
  printf("\r\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
}

// 扫描密码文件和组文件中的所有记录(/etc/passwd 文件信息) 逐条获取(直到NULL)
void LAP_getPWDENT(void)
{
  struct passwd * pwd = getpwent();           // getpwent 函数会自动打开一条密码文件(调用endpwent关闭此文件)
  printf("pw_name:%s\r\n",pwd->pw_name);      // 登录名称
  printf("pw_passwd:%s\r\n",pwd->pw_passwd);  // 登录密码(空字符:无密码 非空:启用了 shadow,者密码存放于/etc/shadow)
  printf("pw_uid:%d\r\n",pwd->pw_uid);        // 用户ID
  printf("pw_gid:%d\r\n",pwd->pw_gid);        // 组ID
  printf("pw_gecos:%s\r\n",pwd->pw_gecos);    // 用户真实名称
  printf("pw_dir:%s\r\n",pwd->pw_dir);        // 家目录
  printf("pw_shell:%s\r\n",pwd->pw_shell);    // 登录shell(若为空，则使用默认shell(/bin/sh))
  printf("\r\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
  endpwent(); // 关闭打开的密码文件
}


#include <sys/types.h>
#include <grp.h>

/* The group structure.
struct group
  {
    char *gr_name;		/* Group name.
    char *gr_passwd;	/* Password
    __gid_t gr_gid;		/* Group ID.	
    char **gr_mem;		/* Member list.	
  };
*/

// 获取用户组信息(/etc/group 文件信息)
void LAP_getGroup(void)
{
  
  struct group * gr = getgrnam("respate");   // getpwnam 与 getpwuid函数不可重入(可重入:getpwnam_r 与 getpwuid_r)
  printf("gr_name:%s\r\n",gr->gr_name);      // 登录名称
  printf("gr_passwd:%s\r\n",gr->gr_passwd);  // 登录密码(空字符:无密码 非空:启用了 shadow,者密码存放于/etc/shadow)
  printf("gr_gid:%d\r\n",gr->gr_gid);        // 用户ID
  printf("\r\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
}





#include <shadow.h>
/* Structure of the password file.
struct spwd
{
    char *sp_namp;		    /* Login name.
    char *sp_pwdp;		    /* Encrypted password.
    long int sp_lstchg;		/* Date of last change. 
    long int sp_min;		  /* Minimum number of days between changes. 
    long int sp_max;		  /* Maximum number of days between changes. 
    long int sp_warn;		  /* Number of days to warn user to change the password.  
    long int sp_inact;		/* Number of days the account may be inactive.
    long int sp_expire;		/* Number of days since 1970-01-01 until ccount expires.
    unsigned long int sp_flag;	/* Reserved.
};

struct spwd *getspnam(const char *name);
struct spwd *getspent(void);
void setspent(void);
*/


// 获取密码记录(/etc/shadow 文件信息)
void LAP_getspnam(void)
{
  struct spwd * pwd = getgrnam("respate");    // 
  printf("sp_namp:%s\r\n",pwd->sp_namp);      // 登录名称
  printf("sp_pwdp:%s\r\n",pwd->sp_pwdp);      // 登录密码(空字符:无密码 非空:启用了 shadow,者密码存放于/etc/shadow)
  printf("sp_lstchg:%d\r\n",pwd->sp_lstchg);  // 用户ID
  printf("\r\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
}


#define _XOPEN_SOURCE       /* See feature_test_macros(7) */
#include <unistd.h>
#include <crypt.h>
// char *crypt(const char *key, const char *salt);
// char *getpass(const char *prompt);
// 控制台获取密码输入&&密码校验
void LAP_getPasswd()
{
  struct passwd* pwd = getpwnam("respate");
  char* in_pwd = getpass("请输入密码:");
  printf("输入的密码:%s\r\n",in_pwd);
  char* des_pwd = (char*)crypt(in_pwd,pwd->pw_passwd); // -lcrypt(链接动态库)
  printf("加密密码:%s\r\n",des_pwd);
  printf("\r\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
}

#if 0
int main(void)
{
    LAP_ErrOutSet(NULL);// 初始化错误输出流
    LAP_getPWNAM();     // 获取用户信息
    //LAP_getPWDENT();    // 扫描密码文件和组文件中的所有记录
    LAP_getGroup();     // 获取组ID

    LAP_getspnam();     // 从shadow获取密码记录
    LAP_getPasswd();
    //while (1);
    return 0;
}
#endif